﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents
{
    /// <summary>
    /// ZoneController.xaml の相互作用ロジック
    /// </summary>
    public partial class ZoneController : UserControl
    {

        #region Constructor
        public ZoneController()
        {
            InitializeComponent();
        }
        #endregion

        #region private member

        private ZoneControllerViewModel ViewModel
        {
            get
            {
                return DataContext as ZoneControllerViewModel;
            }
        }

        private ZoneControllerViewModel inserted = null;
        private readonly object lockInserted = new object();

        #endregion

        #region private events

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.NewValue is ZoneControllerViewModel)
            {
                var old = e.OldValue as ZoneControllerViewModel;

                ViewModel.ParentPropertyChanged += new EventHandler<ZoneControllerViewModel.ParentPropertyChangedEventArgs>(ParentChanged);
                ParentChanged(null, null);
            }
        }

        private void ParentChanged(object sender, ZoneControllerViewModel.ParentPropertyChangedEventArgs e)
        {
            body.Children.Clear();
            this.ViewModel.Composer.DisposeView();       

            if (e == null || e.NewValue != null) //call by DataContextChanged or e.Newvalue != null
            {
                this.ViewModel.Composer.CreateView(this.Dispatcher);
                body.Children.Add(this.ViewModel.Composer.View);
            }
        }

        private void UserControl_PreviewDragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Move;
            //if (!e.Data.GetDataPresent(typeof(ZoneControllerViewModel)) || e.Data.GetData(typeof(ZoneControllerViewModel)) == this.ViewModel)
            //{
            //    e.Effects = DragDropEffects.None;
            //}
        }

        private void UserControl_PreviewDragOver(object sender, DragEventArgs e)
        {
            lock (lockInserted)
            {
                if (inserted != null)
                    return;
            }

            if (e.Data.GetDataPresent(typeof(ZoneControllerViewModel)))
            {
                var srcController = (e.Data.GetData(typeof(ZoneControllerViewModel)) as ZoneControllerViewModel);
                if (this.ViewModel.Parent != srcController.Parent
                    || this.ViewModel.Parent.IndexOf(this.ViewModel) != srcController.Parent.IndexOf(srcController))
                {
                    lock (srcController.Parent.LockObject)
                        srcController.Parent.Remove(srcController);

                    srcController.Parent = this.ViewModel.Parent;
                    lock (srcController.Parent.LockObject)
                        this.ViewModel.Parent.Insert(this.ViewModel.Parent.IndexOf(this.ViewModel), srcController);

                    lock (lockInserted)
                        inserted = srcController;

                    this.InvalidateVisual();
                }
            }
        }

        private void UserControl_PreviewDragLeave(object sender, DragEventArgs e)
        {
            //Console.WriteLine("dragleave start : " + this.TimelineTitle.Text);

            lock (lockInserted)
                inserted = null;

            //Console.WriteLine("dragleave end");

            this.InvalidateVisual();
        }

        //以下二つはスクロール系の動作を阻害させないため
        private void body_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void body_MouseMove(object sender, MouseEventArgs e)
        {
            e.Handled = true;
        }

        #endregion
    }

    public class IsNullValueConverter
        : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
